<template>
  <div class="w">
    <div class="header">
      <i class="iconfont icon-fanhui" @click="back"></i>
      <span class="title">新增收货地址</span>
    </div>
    <div class="info">
      <div class="info_card">
        <el-form inline hide-required-asterisk ref="addressRef" :model="addressForm" :rules="addressRules">
          <div class="form">
            <el-form-item prop="people" label="联系人">
              <el-input style="width: 180px" v-model="addressForm.people" clearable placeholder="请输入姓名" class="input"></el-input>
            </el-form-item>
            <el-form-item prop="sex">
              <el-radio v-model="addressForm.sex" label="0">先生</el-radio>
              <el-radio v-model="addressForm.sex" label="1">女士</el-radio>
            </el-form-item>
          </div>
          <div class="form">
            <el-form-item prop="phone" label="手机号">
              <el-input style="width: 296px" v-model="addressForm.phone" placeholder="请输入手机号" clearable="" class="input"></el-input>
            </el-form-item>
          </div>
          <div class="form">
            <el-form-item prop="address" label="收货地址">
              <el-input style="width: 282px" v-model="addressForm.address" clearable placeholder="请输入收货地址" class="input"></el-input>
            </el-form-item>
          </div>
          <div class="form">
            <el-form-item prop="isDefault" label="设为默认地址">
              <div class="el_switch">
                <el-switch @change="elSwitch" v-model="switchValue" active-color="#FFC200" inactive-color="#DCDFE6"> </el-switch>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <div class="info_button" v-if="active == 1">
        <el-button @click="del" style="background-color: #979797">删除地址</el-button>
      </div>
      <div class="info_button">
        <el-button @click="summit(active)">保存地址</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 手机号验证规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error('手机号不合法'));
    };
    return {
      addressForm: {
        people: '',
        sex: '',
        phone: '',
        address: '',
        username: '',
        userid: '',
        isDefault: 0,
      },
      addressRules: {
        people: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        // 性别验证规则
        sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        phone: [
          { validator: checkPhone, trigger: 'blur' },
          { required: true, message: '手机号不能为空', trigger: 'blur' },
        ],
        address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
      },
      info: [],
      active: 0,
      switchValue: false,
    };
  },

  created() {
    this.info = JSON.parse(window.localStorage.getItem('userInfo'));
    this.AddOrUpdate();
  },

  methods: {
    elSwitch() {
      this.addressForm.isDefault = this.switchValue == true ? '1' : '0';
    },
    AddOrUpdate() {
      if (this.$route.query.addressInfo) {
        this.addressForm = this.$route.query.addressInfo;
        this.addressForm.sex = JSON.stringify(this.addressForm.sex);
        this.active = 1;
        if (this.addressForm.isDefault == 1) {
          this.switchValue = true;
        }
        if (this.addressForm.isDefault == 0) {
          this.switchValue = false;
        }
      } else {
        return;
      }
    },
    back() {
      this.$router.back();
    },
    summit(active) {
      this.$refs.addressRef.validate(async (valid) => {
        if (valid) {
          this.addressForm.username = this.info.name;
          this.addressForm.userid = this.info.id;
          if (active == 0) {
            const { data: res } = await this.$http.post('addressBook/addAddress', this.addressForm);
            if (res.code == 1) {
              this.$router.push('/address');
              return this.$message.success(res.data);
            } else {
              return this.$message.error('添加失败');
            }
          }
          if (active == 1) {
            const { data: res } = await this.$http.put('addressBook/updateAddress', this.addressForm);
            if (res.code == 1) {
              this.$router.push('/address');
              return this.$message.success(res.data);
            } else {
              return this.$message.error('修改失败');
            }
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
    async del() {
      const { data: res } = await this.$http.delete('addressBook/deleteAddress/' + this.addressForm.id);
      if (res.code == 1) {
        this.$router.push('/address');
        return this.$message.success(res.data);
      } else {
        return this.$message.error('删除失败');
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  height: 100vh;
  width: 100%;
  background-color: #fff;
  .header {
    background-color: #333333;
    height: 50px;
    color: #fff;
    text-align: center;
    position: relative;
    .icon-fanhui {
      position: absolute;
      font-size: 25px;
      line-height: 50px;
      left: 10px;
    }
    .title {
      font-size: 20px;
      letter-spacing: 1px;
      line-height: 50px;
    }
  }
  .info {
    padding: 15px;
    .input {
      :deep(.el-input__inner) {
        border: none;
      }
    }
    .info_card {
      .form {
        border-bottom: 2px solid #f5f5f5;
        height: 50px;
        display: flex;
        margin-bottom: 10px;
        justify-content: space-between;
        position: relative;
        :deep(.el-radio) {
          margin-right: 5px;
        }
        :deep(.el-radio__label) {
          padding-left: 5px;
        }
        :deep(.el-form-item__error) {
          position: absolute;
          top: 70%;
          left: 15px;
        }
        .el_switch {
          float: right;
          position: absolute;
          right: -250px;
        }
      }
    }
    .info_button {
      :deep(.el-button) {
        border-radius: 50px;
        background-color: #ffc200;
        width: 100%;
        margin-top: 20px;
        border: none;
        color: #000000;
      }
    }
  }
}
</style>